package com.example.adms.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${system.imageUrl}")
    private String imagePath;


    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //设置静态资源url路径映射
        registry.addResourceHandler("/view/**")//访问路径
                .addResourceLocations("file:"+imagePath+"/");//真实路径

        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").
                addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new SaInterceptor(handle -> {
            // 除登录和验证码接口外，都需要登录
            SaRouter
                    .match("/**")
                    .notMatch("/user/login","/user/vcode")
                    .check(r -> StpUtil.checkLogin());
        }))
                .addPathPatterns("/**")
                .excludePathPatterns("/**/*.png");

    }

}
